package 二叉树系列;

import java.util.ArrayList;
import java.util.List;

public class Traversal {
    //前序
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        preorder(root,result);
        return result;
    }
    public void preorder(TreeNode treeNode,List result){
        if(treeNode==null){
            return ;
        }
        result.add(treeNode.val);
        preorder(treeNode.left,result);
        preorder(treeNode.right,result);
    }
    //后序
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public void postorder(TreeNode treeNode,List result){
        if(treeNode==null){
            return ;
        }
        postorder(treeNode.left,result);
        postorder(treeNode.right,result);
        result.add(treeNode.val);

    }
    //中序
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorder(root,result);
        return result;
    }
    public void inorder(TreeNode treeNode,List result){
        if(treeNode==null){
            return ;
        }
        inorder(treeNode.left,result);
        result.add(treeNode.val);
        inorder(treeNode.right,result);
    }
}
